<?php

namespace tools;
use GuzzleHttp\Client;
use think\exception\ValidateException;
use think\facade\Log;

class HttpPost
{
    /**
     * post请求
     * @param $url
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function post($url,$data){
        $client = new Client();
        $res = $client->request('POST',$url,$data);
        if($res->getStatusCode() != 200) throw new ValidateException('请求失败！');
        $data = $res->getBody()->getContents();
        return json_decode($data,true);
    }

    /**
     * get请求
     * @param $url
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function get($url,$data){
        $client = new Client();
        $res = $client->request('GET',$url,$data);
        if($res->getStatusCode() != 200) throw new ValidateException('请求失败！');
        $data = $res->getBody()->getContents();
        return json_decode($data,true);
    }
}